<template>
	<!-- 用户端显示此页面-否则跳转到服务端页 -->
	<view v-if="configs.beginning == 'user'">
		<!-- 存在分享id则跳转到分享页-需要隐藏此页面过渡 -->
		<view v-if="!shareStudioId">
			<view :class="{ pageHomefixed: loginStatus() }">
				<firstHome ref="firstHomeRef" />
			</view>

			<tabbar currentPages="首页"></tabbar>
			<view v-if="loginStatus()">
				<secondHome :scrollHeight="'calc(100vh - 120rpx)'" ref="secondHomeRef" />
			</view>

			<versionUpdate ref="versionUpdateRef"></versionUpdate>
		</view>
	</view>
</template>

<script setup lang="ts">
	import configs from '@/config';
	import firstHome from './components/firstHome.vue';
	import secondHome from './components/secondHome.vue';
	import { loginStatus } from '@/hooks/useLogin';
	import { ref, onMounted } from 'vue';
	import { onShow, onLoad, onShareAppMessage } from '@dcloudio/uni-app';
	import { useUserStoreToRefs } from '@/store/useUserStore';
	const { userInfo, shareStudioId } = useUserStoreToRefs();
	const firstHomeRef = ref();
	const secondHomeRef = ref();
	const versionUpdateRef = ref()
	onMounted(() => {
		versionUpdateRef.value && versionUpdateRef.value.check();
	});
	onShow(() => {
		if (userInfo.value) {
			firstHomeRef.value && firstHomeRef.value.applyCheck.check();
		}
	})
	onLoad(async (params : any) => {
		if (configs.beginning !== 'user') {
			if (!loginStatus()) {
				uni.reLaunch({
					url: "/pages/login/index"
				});
			} else {
				uni.showToast({
					mask: true,
					icon: "loading",
					title: "加载中",
					duration: 500,
					success: () => {
						uni.redirectTo({
							url: '/pages/myStudio/index/index'
						})
					}
				})
			}
			return
		}
		// shareStudioId.value = '21'
		if (shareStudioId.value) {
			uni.showToast({
				mask: true,
				icon: "loading",
				title: "加载中",
				duration: 800,
				success: () => {
					uni.reLaunch({
						url: `/pages/homeShare/index?shareStoreId=${params.shareStoreId}`
					})
				}
			})
		}
		//先检查网络，有网时执行，无网不显示
		// #ifdef APP
		let s = 0;
		let time = setInterval(() => {
			uni.getNetworkType({
				success: async (res) => {
					if (res.networkType !== 'none') {
						clearInterval(time);
						console.log('清楚网络..');//有网络就加载
						//写逻辑代码，（包含接口文件和网络接口的）
						init()
					} else {
						console.log('没有网络')
					}
				}
			});
			s++;
		}, 1000);
		// #endif
		// #ifndef APP
		init()
		// #endif

	});
	const init = () => {
		console.log('(userInfo', userInfo)
		if (userInfo.value) {
			firstHomeRef.value && firstHomeRef.value.applyCheck.check();
			secondHomeRef.value && secondHomeRef.value.init()
		} else {
			firstHomeRef.value && firstHomeRef.value.bannerFun.getBanner();
			firstHomeRef.value && firstHomeRef.value.getConfig.getConfigContent();
		}
	}

	onShareAppMessage(() => {
		let cans = `${configs.api}/?xcx=xjs&shareRefereeId=${userInfo.value.id || ''}&text=wx&type=2`;
		return {
			title: '555',
			path: `/pages/shareRefereeId/index?q=${encodeURIComponent(cans)}`,
		};
	});
</script>

<style lang="scss" scoped>
	.pageHomefixed {
		position: fixed;
		left: -200vw;
		z-index: 100;
	}
</style>